<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 魔币记录
 * @author weihu
 *
 */
class Magic_service
{
    function __construct()
    {
        $this->ci = & get_instance();
        $this->ci->load->model('User_model');
        $this->ci->load->model('Magic_coins_model');
    }
    
    /**
     * 记录魔币
     * @param int $userid 用户ID
     * @param float $coins 金额
     * @param int $origin 来源（1：注册，2：完善资料，3：申请认证，4：订单交易，5:评论,6:上次图片,7:邀请用户注册,8:邀请的用户申请认证）
     * @param int $type 记录类型（1：加，2：减 ， 默认是1）
     */
    public function recordMagicCoins($userid,$coins,$origin=1,$type=1)
    {
        //修改用户表魔币总值
        $userInfo = $this->ci->User_model->get_by_id($userid,'magic_coins');
        $totalcoins = $userInfo['magic_coins'];
        if ($type == 1)
            $totalcoins += $coins;
        else
            $totalcoins -= $coins;
        $data['magic_coins'] = $totalcoins>0?$totalcoins:0;
        $this->ci->User_model->update_by_id($userid,$data);
        
        //录入记录
        $magicData['userid'] = $userid;
        $magicData['magic_coins'] = $coins;
        $magicData['origin'] = $origin;
        $magicData['type'] = $type;
        $magicData['update_time'] = time();
        $magicData['add_time'] = time();
        $this->ci->Magic_coins_model->insert($magicData);
        return true;
    }
}